package top.ewind.controller.admin.boot;

import java.util.Date;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import top.ewind.controller.common.AdminRootCtrl;
import top.ewind.util.SecurityUtil;

/*@Authorize(name = "2", value = "固件管理")*/
public class BootCtrl extends AdminRootCtrl {

	public void index() {
		int nid = getSessionAttr("nid");
		setAttr("plist",Db.find("select * from device_product where nid = ? order by intime desc",nid));
		render("index.html");
	}
	public void profile() {
		int nid = getSessionAttr("nid");
		String filter = " tbl1.nid ="+nid+SecurityUtil.sqlFilter(getPara("param", ""));
		renderJson(Db.paginate(getParaToInt("page", 1), getParaToInt("rp", 10), "select tbl1.*,tbl2.pname ", " from device_boot tbl1 left join device_product tbl2 on tbl1.pid = tbl2.id where " + filter));
	}
	
	/*@Authorize(name = "2_1", value = "新增")*/
	public void add(){
		int nid = getSessionAttr("nid");
		//setAttr("deviceboot",DeviceBoot.dao.findAll(nid));
		setAttr("plist",Db.find("select * from device_product where nid = ? order by intime desc", nid));
		render("edit.html");
	}
	public void edit(){
		int nid = getSessionAttr("nid");
		Record admin = Db.findById("device_boot",getPara(0));
		if(admin!=null){
			setAttr("deviceboot",admin);
			setAttr("plists",Db.find("select * from device_product where id = ? order by intime desc", admin.getStr("pid")));
			setAttr("plist",Db.find("select * from device_product where nid = ? order by intime desc", nid));
			render("edit.html");
		}
		else renderNull();
	}
	/*sava*/
	public void save(){
		boolean ret = false;
		if(StrKit.notBlank(getPara("deviceboot.id"))){
			Record admin = Db.findById("device_boot", getPara("deviceboot.id"));
			admin.set("name", getPara("name")).set("intime", new Date()).set("pid", getPara("device.pid")).set("version", getPara("version")).set("fileurl", getPara("fileurla"));
			ret = Db.update("device_boot",admin);
		}
		else {
			Record admin = new Record().set("nid", getSessionAttr("nid")).set("name", getPara("name")).set("intime", new Date()).set("pid", getPara("device.pid")).set("version", getPara("version")).set("fileurl", getPara("fileurla"));
			ret = Db.save("device_boot", admin);
		}
		if(ret)renderJson("msg","success");
		else renderJson("msg","保存失败，请重试！");
	}
	
	/*delete*/
	public void delete() {
		boolean ret = false;
		if(StrKit.notBlank(getPara("id"))){
			ret = Db.deleteById("device_boot",getPara("id"));
		}
		if(StrKit.notBlank(getPara("ids"))){
			ret = Db.update("delete from device_boot where id in ("+getPara("ids")+")")>0;
		}
		if (ret)
			renderJson("msg", "success");
		else
			renderJson("msg", "删除失败，请重试！");
	}
}